package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RequestMapping("/test") // 路由注册
//@Controller // 把类注册到框架中
//@ResponseBody // 告诉程序返回的是数据而非页面
@RestController //和上面两个效果相同
public class TestController {
    @PostMapping("/hi")
    public String sayHi(){
        return "Hi,Spring boot~";
    }
    @GetMapping("/show-user")
    public String showUser(User user) {
        return user.toString();
    }

    @PostMapping("/show-json-user")
    public String showJSONUser(@RequestBody User user){
        return user.toString();
    }

    @RequestMapping("/param9")
    public String param9(@RequestPart("myfile") MultipartFile file) throws IOException {
        String path = "D:\\text3\\img.png";
        file.transferTo(new File(path));
        return path;
    }
    @RequestMapping("/param10")
    public String param10(@RequestPart("myfile") MultipartFile file) throws IOException {
        // 根目录
        String path = "D:\\text3\\";
        // 根目录 + 唯一文件名
        path += UUID.randomUUID().toString().replace("-","");
        // 根目录 + 唯一文件名 + 文件后缀
        path += file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        file.transferTo(new File(path));
        return path;
    }
}
